package com.bc.web.controller.admin;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.bc.web.model.Channel;
import com.bc.web.model.Dictionary;
import com.bc.web.model.FeeModel;
import com.bc.web.model.ShareFee;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

public class ShareFeeController extends FSController {

	public void search() {
		Page<ShareFee> items = ShareFee.dao.search(super.getSearchCondition());
		List<Channel> channelList = Channel.dao.selectAll();
		List<FeeModel> feeModelList = FeeModel.dao.selectAll();
		List<Dictionary> payTypeList = Dictionary.dao.selectByParentKey("payType");
		setAttr("channelList", channelList);
		setAttr("feeModelList", feeModelList);
		setAttr("payTypeList", payTypeList);
		setPageResultToRequest("items", items);
		render("fee/share/shareFeeList.html");
	}

	/**
	 * 固定分润费率页面
	 */
	public void addFixedPage() {
		List<FeeModel> feeModelList = FeeModel.dao.selectAll();
		List<Dictionary> payTypeList = Dictionary.dao.selectByParentKey("payType");
		List<Channel> channelList = Channel.dao.selectAll();
		setAttr("feeModelList", feeModelList);
		setAttr("payTypeList", payTypeList);
		setAttr("channelList", channelList);
		render("fee/share/shareFeeFixedAdd.html");
	}
	
	/**
	 * 非固定分润费率页面
	 */
	public void addNotFixedPage() {
		List<FeeModel> feeModelList = FeeModel.dao.selectAll();
		List<Dictionary> payTypeList = Dictionary.dao.selectByParentKey("payType");
		List<Channel> channelList = Channel.dao.selectAll();
		setAttr("feeModelList", feeModelList);
		setAttr("payTypeList", payTypeList);
		setAttr("channelList", channelList);
		render("fee/share/shareFeeNotFixedAdd.html");
	}

	public void updatePage() {
		ShareFee shareFee = ShareFee.dao.findById(getPara("id"));
		setAttr("shareFee", shareFee);
		List<FeeModel> feeModelList = FeeModel.dao.selectAll();
		List<Dictionary> payTypeList = Dictionary.dao.selectByParentKey("payType");
		List<Channel> channelList = Channel.dao.selectAll();
		setAttr("feeModelList", feeModelList);
		setAttr("payTypeList", payTypeList);
		setAttr("channelList", channelList);
		render("fee/share/shareFeeUpdate.html");
	}

	@Before(Tx.class)
	public void add() {
		String merchCode = getPara("merchCode");
		String[] channelList = getParaValues("channelCode");
		String[] payTypeList = getParaValues("payTypeCode");
		String[] orgCodeList = getParaValues("orgCode");
		String[] isFixedList = getParaValues("isFixed");
		String[] shareFeeIdList = getParaValues("shareFeeId");
		if (null == payTypeList || payTypeList.length == 0) {
			renderFail("请添加行.");
			return;
		}
		
		Set<String> tmpSet = new HashSet<>();
		for (String payType : payTypeList) {
			tmpSet.add(payType);
		}
		if (tmpSet.size() < payTypeList.length) {
			renderFail("支付类别不允许重复.");
			return;
		} 
		
		for (int i = 0; i < payTypeList.length; i++) {
			boolean isFixed = Boolean.valueOf(isFixedList[i]);
			ShareFee shareFee = null;
			if (isFixed) {
				shareFee = ShareFee.dao.selectIsFixed(merchCode, channelList[i], orgCodeList[i], payTypeList[i]);
			} else {
				shareFee = ShareFee.dao.selectIsNotFixed(channelList[i], orgCodeList[i], payTypeList[i]);
			}
			
			if (shareFee != null) {
				shareFee.setIsFixed(isFixed);
				shareFee.setShareFeeId(Integer.valueOf(shareFeeIdList[i]));
				shareFee.setUpdateTime(FSDate.getDateTime());
				shareFee.update();
			} else {
				shareFee = new ShareFee();
				shareFee.setChannelCode(channelList[i]);
				shareFee.setMerchCode(merchCode);
				shareFee.setPayTypeCode(payTypeList[i]);
				shareFee.setOrgCode(orgCodeList[i]);
				shareFee.setShareFeeId(Integer.valueOf(shareFeeIdList[i]));
				shareFee.setIsFixed(isFixed);
				shareFee.setUpdateTime(FSDate.getDateTime());
				shareFee.save();
			}
		}
		renderSuccess();
	}

	@Before(Tx.class)
	public void update() {
		ShareFee shareFee = getModel(ShareFee.class, true);
		shareFee.update();
		renderSuccess();
	}

	@Before(Tx.class)
	public void delete() {
		ShareFee s = ShareFee.dao.findById(getPara("id"));
		ShareFee.dao.deleteById(s.getId());
		renderSuccess(false);
	}

}
